package com.acme.bankapp.domain.account;

public class CheckingAccount extends AbstractAccount{

    private static final String ERROR_OVERDRAFT = "Cannot set overdraft smaller than 0!";

    private final double overdraft;
    private double currentOverdraft;

    public CheckingAccount(final int id, final double amount, final double overdraft) {
        super(id, amount);
        if (overdraft < 0) {
            throw new IllegalArgumentException(ERROR_OVERDRAFT);
        }
        this.overdraft = overdraft;
        this.currentOverdraft = overdraft;
    }

    @Override
    public double maximumAmountToWithdraw() {
        return super.maximumAmountToWithdraw() + currentOverdraft;
    }

    @Override
    public void withdraw(double amount) {
        if (amount <= 0) { return; }

        if (amount <= balance) {
            balance -= amount;
            return;
        }

        if (amount <= (balance + currentOverdraft)) {
            amount -= balance;
            balance = 0;
            currentOverdraft -= amount;
        }
    }

    @Override
    public void deposit(double amount) {
        if (amount <= 0) { return; }

        double delta = overdraft - currentOverdraft;
        if (amount <= delta) {
            currentOverdraft += amount;
        } else {
            amount -= delta;
            currentOverdraft = overdraft;
            balance += amount;
        }
    }

    @Override
    public String toString() {
        return super.toString() + " [overdraft: " + currentOverdraft + "]";
    }
}
